<?php

error_reporting(1);
$link = null;
$directory = "controlador/";

if(!is_dir($directory)){
    mkdir($directory);
}
/////CONEXION BD
if($link = mysql_connect("localhost", "root", "")) {
    if (!mysql_select_db("g2u", $link)){
        die ("la base de datos no existe.");
    }
} else {
   die ("error al conectar al motor."); 
}


/////OBTENEMOS LAS TABLAS DESDE BD
$rs = null;
$fila = null;
$rs = mysql_query("SHOW TABLES;", $link);
$tables = array();
if (is_resource($rs)){
    
    while($fila = mysql_fetch_array($rs)){
        $tables[]= $fila[0];
    }
    
} else {
    die ("LA BD NO TIENE TABLAS");
}



foreach ($tables as $key => $value) {
    $rs = null;
    $fila = null;
    $deleteByUpdate = false;
    $fieldStatus = null;
    $stringCampos = "";
    $fieldDesc = "";
    $statusIndex = "";
    
    $rs = mysql_query("DESC $value;", $link);
    $campos = array();

    while($fila = mysql_fetch_array($rs)){
        $campos[]= $fila[0];
        $stringCampos .= " $fila[0],"; 
        
        if(strpos($fila[0], "status")){
            $deleteByUpdate = true;
            $fieldStatus = $fila[0];
            $statusIndex = count($campos)-1;
        }
        
        if(strpos($fila[0], "desc")){
            $fieldDesc = $fila[0];            
        }
    }
    
    $stringCampos = substr($stringCampos, 0, strlen($stringCampos)-1);
    $value2 = str_replace("_", "", $value);
    $value2 = strtoupper(substr($value2, 0, 1)).substr($value2, 1, strlen($value2));
/////CREANDO ARCHIVOS DE CLASE
    $file = $directory."controlador.$value.php";
    $fp = fopen($file, 'w');
    echo date('Y-m-d H:i:s')."  ::  Create File:".$file;

    fwrite($fp, 
'<?php
class Controller'.  strtoupper(substr($value, 0, 1)).substr($value, 1, strlen($value)).' { 

    function getFunction($op){
        
        switch ($op) {
            case "new":
                $this->new'.$value2.'();
                break;
            
            case "list":
                $this->list'.$value2.'();
                break;
            
            case "save":
                $this->save'.$value2.'();
                break;

            case "edit":
                $this->edit'.$value2.'();
                break;
            
            case "update":
                $this->update'.$value2.'();
                break;
            
            case "del":
                $this->delete'.$value2.'();
                break;
            
            case "combo":
                return $this->combo'.$value2.'();
                break;
            
            default:
                echo "ERROR NO HAY CONTROLADOR PARA ESA OPCION";
                break;
        }
    }
    
');

    fwrite($fp, 
'
    function new'.$value2.'(){
        include_once "vista/vista.'.$value.'.php";
        form_'.$value.'();
    }    
    
');
    
    fwrite($fp, 
'
    function save'.$value2.'(){
        include_once "modelo/modelo.'.$value.'.php";
        
        $'.substr($value, 0, 1).' = new '.$value.'();
');
foreach ($campos as $k => $v) {    
    fwrite($fp, 
'        $'.substr($value, 0, 1).'->set_'.$v.'($_REQUEST["'.$v.'"]); 
');
}

    fwrite($fp, 
'
        
        if($'.substr($value, 0, 1).'->Save()){
            $msj = "<div id=\'alertzone\' style=\'".$_SESSION[\'alert\'][\'style_ok\']." \'> <img src=\'".$_SESSION[\'alert\'][\'image_ok\']."\' /> ".$_SESSION[\'alert\'][\'msj_save_ok\']." </div>";
        }else{
            $msj = "<div id=\'alertzone\' style=\'".$_SESSION[\'alert\'][\'style_error\']."\'> <img src=\'".$_SESSION[\'alert\'][\'image_error\']."\' /> ".$_SESSION[\'alert\'][\'msj_save_error\']." </div>";
        }
        $this->list'.$value2.'($msj);
        
    }    
');
    
    fwrite($fp, 
'    
    function edit'.$value2.'(){
        include_once \'modelo/modelo.'.$value.'.php\';
        include_once \'vista/vista.'.$value.'.php\';
        
        $'.substr($value, 0, 1).' = new '.$value.'();
        $'.substr($value, 0, 1).'->set'.$campos[0].'($_REQUEST["'.$campos[0].'"]); 
        
        $'.substr($value, 0, 1).'->getData();
        
        form_'.$value.'($'.substr($value, 0, 1).');
        
    }    
');
    

    fwrite($fp, 
'    
    function delete'.$value2.'(){
        include_once \'modelo/modelo.'.$value.'.php\';
        
        $'.substr($value, 0, 1).' = new '.$value.'();
        $'.substr($value, 0, 1).'->set'.$campos[0].'($_REQUEST["'.$campos[0].'"]); 
        
        if($'.substr($value, 0, 1).'->Delete()){
            $msj = "<div id=\'alertzone\' style=\'".$_SESSION[\'alert\'][\'style_ok\']." \'> <img src=\'".$_SESSION[\'alert\'][\'image_ok\']."\' /> ".$_SESSION[\'alert\'][\'msj_delete_ok\']." </div>";
        }else{
            $msj = "<div id=\'alertzone\' style=\'".$_SESSION[\'alert\'][\'style_error\']."\'> <img src=\'".$_SESSION[\'alert\'][\'image_error\']."\' /> ".$_SESSION[\'alert\'][\'msj_delete_error\']." </div>";
        }
        $this->list'.$value2.'($msj);
    }

');    
    
    
    
    fwrite($fp, 
'    
    function update'.$value2.'(){
        include_once \'modelo/modelo.'.$value.'.php\';
        
        $'.substr($value, 0, 1).' = new '.$value.'();
');
foreach ($campos as $k => $v) {    
    fwrite($fp, 
'        $'.substr($value, 0, 1).'->set_'.$v.'($_REQUEST["'.$v.'"]); 
');
}

    fwrite($fp, 
'
        if($'.substr($value, 0, 1).'->Update()){
            $msj = "<div id=\'alertzone\' style=\'".$_SESSION[\'alert\'][\'style_ok\']." \'> <img src=\'".$_SESSION[\'alert\'][\'image_ok\']."\' /> ".$_SESSION[\'alert\'][\'msj_update_ok\']." </div>";
        }else{
            $msj = "<div id=\'alertzone\' style=\'".$_SESSION[\'alert\'][\'style_error\']."\'> <img src=\'".$_SESSION[\'alert\'][\'image_error\']."\' /> ".$_SESSION[\'alert\'][\'msj_update_error\']." </div>";
        }
        $this->list'.$value2.'($msj);
    }

');
    
    fwrite($fp, 
'    
    function list'.$value2.'($msj=false){
        include_once \'modelo/modelo.'.$value.'.php\';
        include_once \'vista/vista.'.$value.'.php\';
        
        $'.substr($value, 0, 1).' = new '.$value.'();

        $'.substr($value, 0, 1).'->set'.$campos[0].'($_REQUEST["'.$campos[0].'"]); 
        $'.substr($value, 0, 1).'->getData();
        
        $dataTotal = $'.substr($value, 0, 1).'->GetList(" count(*) ", false,  true);
        $dataTotal = $dataTotal[0][0]; 
        $data = $'.substr($value, 0, 1).'->GetList(" * ", false);
        if(is_null($data)){
            $msj = "<div id=\'alertzone\' style=\'".$_SESSION[\'alert\'][\'style_error\']."\'> <img src=\'".$_SESSION[\'alert\'][\'image_error\']."\' /> ".$_SESSION[\'alert\'][\'msj_empty_error\']." </div>";
        }
        list_'.$value.'($data, $msj, $dataTotal);

    }
');
        
    
    
/////CERRAR CLASE
fwrite($fp, 
"
}");         
     
    fclose($fp);
    echo " <br/>";
    echo date('Y-m-d H:i:s')."  ::  End creation File:".$file;
    echo " <br/><br/>";    
}


    

    